<?php

/**
 * 模型对应数据库表 "web_price".
 *
 * 以下是可用表web_price的栏位:
 * @property integer $id
 * @property integer $site_id
 * @property integer $game_id
 * @property integer $zone_id
 * @property string $product_type
 * @property string $where
 * @property string $function
 * @property string $state
 */
class WebPrice extends CActiveRecord {

    /**
     * 返回指定的基于“ar”的静态模型类.
     * @return WebPrice the static model class
     * @author nirsh
     * @example
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * 返回数据库表的名.
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function tableName() {
        return 'web_price';
    }

    /**
     * 返回验证规则
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function rules() {
        return array(
            array('site_id,function', 'required'),
            array('site_id, game_id, zone_id', 'numerical', 'integerOnly' => true),
            array('product_type', 'length', 'max' => 50),
            array('state', 'length', 'max' => 1),
            array('where, function', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, site_id, game_id, zone_id, product_type, where, function, state', 'safe', 'on' => 'search'),
        );
    }

    /**
     * 返回关联规则数组。
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * 通过ID获取名称
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function getNameById($id, $nameField) {
        $res = $this->findByPk($id);
        return $res[$nameField] ? $res[$nameField] : '';
    }

    /**
     * 返回属性标签(名称= >标签)数组
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function attributeLabels() {
        return array(
            'id' => '系统编号',
            'site_id' => '网站',
            'game_id' => '游戏',
            'zone_id' => '服务区',
            'product_type' => '产品类型',
            'where' => '条件',
            'function' => '计算公式',
            'state' => '状态',
        );
    }

    /**
     * 设置查询相关条件
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('site_id', $this->site_id);
        $criteria->compare('game_id', $this->game_id);
        $criteria->compare('zone_id', $this->zone_id);
        $criteria->compare('product_type', $this->product_type, true);
        $criteria->compare('where', $this->where, true);
        $criteria->compare('function', $this->function, true);
        $criteria->compare('state', $this->state, true);

        return new CActiveDataProvider(get_class($this), array(
            'criteria' => $criteria,
        ));
    }

    /**
      +-----------------------------------------------------
     * 说明 获取价格策略
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getPriceFunction($game='', $server='', $ptype='') {
        $cdbc = new CDbCriteria;
        $cdbc->compare('site_id', Yii::app()->params->site_id);
        if ($game) {
            $codition = "game_id='" . $game . "'";
            if ($this->isHavePriceFunction($codition)) {
                $cdbc->compare('game_id', $game);
            } else {
                $gObj = ProductCategory::model()->findByPk($game);
                $codition = "game_id='" . $gObj->parentid . "'";
                if ($this->isHavePriceFunction($codition)) {
                    $cdbc->compare('game_id', $gObj->parentid);
                } else {
                    $cdbc->compare('game_id', '0');
                }
            }
        }
        if ($server) {
            $codition = "zone_id='" . $server . "'";
            if ($this->isHavePriceFunction($codition)) {
                $cdbc->compare('zone_id', $server);
            } else {
                $cdbc->compare('zone_id', '0');
            }
        }
        if ($ptype) {
            $codition = "product_type='" . $ptype . "'";
            if ($this->isHavePriceFunction($codition)) {
                $cdbc->compare('product_type', $ptype);
            } else {
                $cdbc->compare('product_type', '0');
            }
        }
        $cdbc->compare('state', 1);
        $cdbc->order = "id desc";
        return $this->find($cdbc);
    }

    /**
      +-----------------------------------------------------
     * 说明 判断是否存在策略
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function isHavePriceFunction($condition) {
        $cdbc = new CDbCriteria;
        $cdbc->compare('site_id', Yii::app()->params->site_id);
        $cdbc->compare('state', 1);
        $cdbc->addCondition($condition);
        $DATA = $this->find($cdbc);
        return $DATA->id ? true : false;
    }

    /**
      +-----------------------------------------------------
     * 说明 获取所以条件
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getAllFunction() {
        $cdbc = new CDbCriteria;
        $cdbc->compare('state', 1);
        $cdbc->compare('site_id', Yii::app()->params->site_id);
        return $this->findAll($cdbc);
    }

    /**
      +-----------------------------------------------------
     * 说明 获取折扣价格  
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getFunctionPrice($p, $game='', $server='', $ptype='') {
        $fObj = $this->getPriceFunction($game, $server, $ptype);
        if (empty($fObj)) {
            return $p;
        } else {
            if (!$fObj->where || eval("return $fObj->where;")) {
                $pfunction = $p . $fObj->function;
                return eval("return $pfunction;");
            } else {
                return $p;
            }
        }
    }

}